<include file="Public/header"/>
<include file="Public/menu"/>
<style type="text/css">
    .edit-batch-list .ebatch-name{
        width:480px;
    }
    .edit-batch-list .ebatch-txt{
        width:480px;
    }
</style>
<!-- 内容页 -->
<div class="content-wrapper">
    <!-- 面包屑 -->
    <section class="content-header">
        <h1>
            营销
            <small>管理</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="{:U('GoodsPromotion/index')}"><i class="fa fa-dashboard"></i> 营销</a></li>
            <li class="active">订单促销</li>
        </ol>
    </section>

    <!-- 内容 -->
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-info">
                    <div class="box-header with-border">
                        <h3 class="box-title">新增商品促销</h3>

                        <div class="box-tools">

                            <div class="btn-group pull-right" style="margin-right: 10px">
                                <a href="/Qgs/GoodsPromotion/index" class="btn btn-sm btn-default"><i
                                        class="fa fa-list"></i>&nbsp;商品促销列表</a>
                            </div>
                        </div>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form id="form" action="/Qgs/GoodsPromotion/save" method="post" accept-charset="UTF-8" class="form-horizontal">
                        <div class="box-body">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">名称</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        <input type="hidden" value="{$msg.id}" name="id">
                                        <input name="name" value="{$msg.name}" type="text" class="form-control" placeholder="名称" />
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">描述</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        <textarea name="description" class="form-control" placeholder="描述">{$msg.description}</textarea>
                                    </div>
                                </div>
                            </div>
                            <!--<div class="form-group">
                                <label class="col-sm-2 control-label">排他功能</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        否<input name="paita" value="0" type="radio" <if condition="$msg['paita'] eq '0'">checked</if>>
                                        是 <input name="paita" value="1" type="radio" <if condition="$msg['paita'] eq '1'">checked</if>>
                                    </div>
                                </div>
                            </div>-->
                            <input name="paita" value="1" type="hidden" class="" checked>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">启用状态</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        否<input name="display" value="0" type="radio" <if condition="$msg['display'] eq '0'">checked</if>>
                                        是 <input name="display" value="1" type="radio" <if condition="$msg['display'] eq '1'">checked</if>>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">排序</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        <input name="sort" value="{$msg.sort}" class="form-control ajax-sort" type="text" placeholder="越小越靠前">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">促销条件</label>
                                <div class="col-sm-6">
                                    <div class="no-margin">
                                        <select class="form-control  select2-hidden-accessible select_option" name="type_question">
                                            <option value="1" <if condition="$msg['type_question'] eq '1'">selected="selected"</if> >指定商品</option>
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group question_select"
                            <if condition='$msg["type_question"] eq 1'>style="display:block"
                                <else/>
                                style="display:none"
                            </if>
                            >
                            <label class="col-sm-2 control-label">设置商品</label>
                            <div class="col-sm-6">
                                <div class="no-margin">
                                    <button type="button"  class="btn btn-default pull-left" id="daterange-btn">
                                            <span>
                                              <i class="fa fa-calendar"></i> 点击添加商品
                                            </span>
                                        <i class="fa fa-caret-down"></i>
                                    </button>
                                </div>
                            </div>
                        </div>
                        <div class="form-group question_select"
                        <if condition='$msg["type_question"] eq 1'>style="display:block"
                            <else/>
                            style="display:none"
                        </if>
                        >
                        <div class="col-sm-offset-2 col-sm-6">
                            <div class="box box-primary  col-sm-6">
                                <!-- /.box-header -->
                                <div class="box-body">
                                    <ul class="todo-list ui-sortable">
                                        <?php if ($msg['type_question'] == '1' && !empty($goods)){
                                                    foreach($goods as $v) {

                                                ?>
                                        <li>
                                            <input type="hidden" name="goods_id[]" value="<?php echo $v['store_pid'];?>">
                                            <?php $v_tit = $v['p_name'] . $v['spec_info']; ?>
                                            <a href="javascript:;" title="{$v_tit}" class="text ebatch-name" style="width:300px;">{$v_tit}</a>
                                            <h  style="margin-right: 50px">售价:{$v.price}元 </h>
                                            <h class="result_price_one" style=" <if condition="$msg['type_result'] neq '1'">display:none;</if> color:red;" >促销价:<input type="text"  style="width: 60px ;color: black"  class="is_price" price="{$v.price}" name="promotion_price[]" value="{$v.promotion_price}">元</h>
                                            <input type="hidden"  name="current_price[]" value="{$v.price}"/>
                                            <div class="tools">
                                                <i class="fa fa-trash-o icondel" data-id="<?php echo $v['store_pid'];?>" onclick="delGoodsItem(this)"></i>
                                            </div>
                                        </li>
                                        <?php } } ?>
                                    </ul>
                                </div>
                                <input id="yc-goods-ids" name="question_goods_id_array" value="{$msg.question_goods_id_array}" type="hidden"/>
                                <!-- /.box-body -->
                            </div>
                        </div>
                </div>



                <!--促销结果-->
                <div class="form-group">
                    <label class="col-sm-2 control-label">促销结果</label>
                    <div class="col-sm-6">
                        <div class="no-margin">
                            <select class="form-control result-select"name="type_result">
                                <option value="1" <if condition="$msg['type_result'] eq '1'">selected="selected"</if> >符合条件的指定商品自定义促销价</option>
                                <option value="2" <if condition="$msg['type_result'] eq '2'">selected="selected"</if> >符合条件的指定商品减去固定价格</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group result_price"
                <if condition='$msg["type_result"] eq 2'>style="display:block"
                    <else/>
                    style="display:none"
                </if>
                >
                <label class="col-sm-2 control-label">减去</label>
                <div class="col-sm-6">
                    <div class="input-group">
                        <input class="form-control  select2-hidden-accessible" name="result_price" value="{$v.promotion_price_guding}">
                        <span class="input-group-addon">元</span>
                    </div>
                </div>
            </div>

            <!--促销结果底部-->
            <div class="form-group">
                <label class="col-sm-2 control-label">开始时间</label>
                <div class="col-sm-6">
                    <div class="input-group">
                        <input id="start_time" name="start_time" class="form-control" type="text" value="{:date("Y-m-d H:i:s",$msg['start_time'])}" readonly />
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label">结束时间</label>
                <div class="col-sm-6">
                    <div class="input-group">
                        <input id="end_time" name="end_time" class="form-control" type="text" value="{:date("Y-m-d H:i:s",$msg['end_time'])}" readonly />
                    </div>
                </div>
            </div>
        </div>
        <!-- /.box-body -->

        <div class="box-footer">
            <div class="col-sm-2"></div>
            <div class="col-sm-6">
                <div class="btn-group pull-right">
                    <button type="submit" class="btn btn-info pull-right submit_click">提交</button>
                </div>
            </div>
        </div>
        <!-- /.box-footer -->
        </form>
</div>

</div>
</div>

</section>
</div>


<!-- 底部 -->
<include file="Public/common_footer"/>
<script src="/Public/layer/layer.js"></script>
<link rel="stylesheet" href="/Public/admin/jedate/jedate.css" />
<script src="/Public/admin/jedate/jedate.js"></script>
<script type="text/javascript">
    // 设置时间
    function setJedate(){
        //不显示秒的解决办法
        var stxt = $("#start_time").val().trim();
        if(stxt.length == 19){
            $("#start_time").val(stxt.substr(0, 16));
        }
        var etxt = $("#end_time").val().trim();
        if(etxt.length == 19){
            $("#end_time").val(etxt.substr(0, 16));
        }
        var start = {
            dateCell: '#start_time',
            format: 'YYYY-MM-DD hh:mm',
            minDate: '1970-01-01 00:00:00',
            festival: false,
            maxDate: '2099-12-31 23:59:59',
            isinitVal:true,
            isTime: true,
            choosefun: function(datas) {
                end.minDate = datas;
            }
        };
        var end = {
            dateCell: '#end_time',
            format: 'YYYY-MM-DD hh:mm',
            minDate: '1970-01-01 00:00:00',
            festival: false,
            maxDate: '2099-12-31 23:59:59',
            isinitVal:true,
            isTime: true,
            choosefun: function(datas) {
                start.maxDate = datas;
            }
        };
        jeDate(start);
        jeDate(end);
    }
    setJedate();
    var ychser = new YcChoser();
    var chids = parent.$("#yc-goods-ids").val();
    ychser.setChosenIds(chids);
    function delGoodsItem(o) {
        ychser.isRepeat($(o).attr("data-id"));
        ychser.remove();
        $("#yc-goods-ids").val(ychser.getChosenIds());
        $(o).parents("li").remove();
    }
    function updateGoodsIds(ids) {
        $("#yc-goods-ids").val(ids);
        ychser.setChosenIds(ids);
    }
    $('#daterange-btn').click(function () {
        layer.open({
            type: 2,
            title: '添加商品',
            area: ['1400px', '830px'],
            fixed: false,
            maxmin: true,
            content: "{:U('productAdd','' , '')}"
        });

    });

    $('.select_option').change(function () {
        var v = $(this).val();
        if (v == 1) {
            $('.question_select').show();
            $('.question_man_price_two').hide();
        }
        if (v == 2) {
            $('.question_select').hide();
            $('.question_man_price_two').show();
        }
        if (v == 3) {
            $('.question_select').hide();
            $('.question_man_price_two').hide();
        }
    });
    $('.result-select').change(function () {
        var v = $(this).val();
        if (v == 1) {
            $('.result_price_one').show();
            $('.result_price').hide();
        }
        if (v == 2) {
            $('.result_price').show();
            $('.result_price_one').hide();
        }
    });

    $(".is_price").on('blur',function()
    {var getFloatStr = function(num){
        num += '';
        num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符

        if(/^0+/) //清除字符串开头的0
            num = num.replace(/^0+/, '');
        if(!/\./.test(num)) //为整数字符串在末尾添加.00
            num += '.00';
        if(/^\./.test(num)) //字符以.开头时,在开头添加0
            num = '0' + num;
        num += '00';        //在字符串末尾补零
        num = num.match(/\d+\.\d{2}/)[0];
        return num;
    };
        var num = parseFloat($(this).val());
        var numprice = parseFloat(getFloatStr(num))
        var price = $(this).attr('price');
//        if(num==''){
//            alert('促销价格不能为空!')
//            return false;
//        }
        if(numprice>=price){
            alert('促销价不能大于等于售价')
            $(this).val(price-0.01);
            return false;
        }


    })
    $("#form").validate({
        ignore: "",
        rules: {
            name:{
                required: true,
                rangelength:[2, 50]
            },
            start_time:{
                required: true,
                isActTime: true
            },
            end_time:{
                required: true,
                isActTime: true,
                dateTimeBigThan: "#start_time"
            }
        },
        messages:{
            name:{
                required: "请输入优惠券名称"
            },
            start_time:{
                required: "请输入开始时间"
            },
            end_time:{
                required: "请输入结束时间",
                dateTimeBigThan: "【注意：】结束时间不能小于开始时间"
            }
        },
        submitHandler: function(form){
            form.submit();   //提交表单
        }
    });

</script>
